/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.references;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.module.Module;
import com.googlecode.sca4idea.psi.SCAComponentTagPsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * ReferenceProvider for resolving target values.
 *
 * @author paul.jones
 */
public class AccessWebReferenceProvider extends AbstractReferenceProvider {
    @NotNull
    public PsiReference[] getReferencesByString(String s, PsiElement psiElement, int i) {
        return new PsiReference[] {
              new PsiReferenceBase<PsiLiteralExpression>((PsiLiteralExpression)psiElement) {
                    public boolean isSoft() {
                        return AccessWebReferenceProvider.this.isSoft();
                    }

                    public Object[] getVariants() {
                        SCAComponentTagPsiElement currentComponent = getComponentForCurrentElement(getElement());
                        if (currentComponent == null) return new Object[0];

                        return currentComponent.getVariantsOfReferences(false);
                    }

                    public PsiElement resolve() {
                        SCAComponentTagPsiElement currentComponent = getComponentForCurrentElement(getElement());
                        if (currentComponent == null) return null;

                        return currentComponent.resolveReference(getCanonicalText());
                    }
              }
        };        
    }

    @Nullable
    private SCAComponentTagPsiElement getComponentForCurrentElement(PsiLiteralExpression expr) {
        Module m = ModuleUtil.findModuleForPsiElement(expr);
        if (m == null) {
            return null;
        }

        /*GlobalSearchScope scope = m.getModuleWithDependentsScope();
        VirtualFile componentType =
                ResourceFileUtil.findResourceFileInScope("WEB-INF/web.componentType", expr.getProject(), scope);
        FileRe

        return ComponentResolver.getSCAComponentInstance(componentTag);*/
        return null;
    }
}